package top.oylan.manager.controller.mp;

import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Comment;
import top.oylan.manager.entity.extend.CommentCondition;
import top.oylan.manager.entity.mp.MpResult;
import top.oylan.manager.service.CommentService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-25
 */
@RestController
@RequestMapping("/mp/comment")
@CrossOrigin
public class CommentController {

    @Resource
    CommentService commentService;

    @RequestMapping(value = "/recommend",method = RequestMethod.GET)
    public MpResult recommend(@RequestParam("productId")Integer productId,@RequestParam(value = "userId",defaultValue = "0")Integer userId){
        List<Comment> comments = commentService.selectRecommend(productId,userId);
        MpResult<Object> objectMpResult = new MpResult<>();
        objectMpResult.setData(comments);
        return objectMpResult;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public MpResult page(@RequestParam("productId")Integer productId,
                         @RequestParam(value = "userId",defaultValue = "0")Integer userId,
                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "size" ,defaultValue = "12")Integer size){
        MpResult<Object> objectMpResult = new MpResult<>();
        Pager pager = new Pager();
        pager.setCurrentPage(page);
        pager.setEveyPageRow(size);
        CommentCondition comment = new CommentCondition();
        comment.setCurrentUserId(userId);
        comment.setProductId(productId);
        pager.setCondition(comment);
        List<Comment> comments = commentService.selectByPager(pager);
        pager.setList(comments);
        objectMpResult.setData(pager);
        return objectMpResult;
    }
}
